package cn.sirun.com.fca.personal.center.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.iov.util.ResourcesUtils;

import java.util.List;

import cn.sirun.com.fca.account.constant.AccountActivityConst;
import cn.sirun.com.fca.account.model.base.CarInfoDetailResponse;
import cn.sirun.com.fca.personal.center.R;
import cn.sirun.com.fca.personal.center.constant.PersonalCenterActivityConst;
import cn.sirun.com.fca.personal.center.model.service.PersonalInfoManager;
import cn.sirun.com.fca.personal.center.widget.RecyclerView;

public class CarManagerRecyclerAdapter extends RecyclerView.Adapter<CarManagerRecyclerAdapter.MyViewHolder> {


    private Context mContext;
    private List<CarInfoDetailResponse> mCarList;
    private String mCurCarDin;

    public CarManagerRecyclerAdapter(Context context) {
        this.mContext = context;
    }

    public void setmCarList(List<CarInfoDetailResponse> mCarList, String mCurCarDin) {
        this.mCarList = mCarList;
        this.mCurCarDin = mCurCarDin;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext)
                .inflate(R.layout.car_manager_item_list, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        CarInfoDetailResponse carInfo = mCarList.get(position);
        String vin = carInfo.getVin();
        String status = "";
        int availableStatus = carInfo.getAvailableStatus();
        if (availableStatus == 0) {
            status = ResourcesUtils.getString(R.string.id_not_verified);
            holder.mStatusIV.setBackgroundResource(R.drawable.ic_car_manager_unauthorized);
            holder.mCurCarIV.setVisibility(View.GONE);
            holder.mAuthIV.setVisibility(View.VISIBLE);
            holder.mAuthTV.setText(R.string.car_manager_auth_car);
            holder.mAuthTV.setTextColor(ResourcesUtils.getColor(R.color.blue_5C));
            holder.mAuthTV.setEnabled(true);
        } else if (availableStatus == 1) {
            status = ResourcesUtils.getString(R.string.verifying);
            holder.mStatusIV.setBackgroundResource(R.drawable.ic_car_verifying);
            holder.mAuthTV.setText(R.string.car_manager_verifying);
            holder.mAuthTV.setTextColor(ResourcesUtils.getColor(R.color.grey_88));
            holder.mCurCarIV.setVisibility(View.GONE);
            holder.mAuthIV.setVisibility(View.GONE);
            holder.mAuthTV.setEnabled(false);
        } else if (availableStatus == 2) {
            status = ResourcesUtils.getString(R.string.id_verified);
            holder.mStatusIV.setVisibility(View.GONE);
            holder.mAuthTV.setText(R.string.car_manager_setting_cur_car);
            holder.mAuthTV.setTextColor(ResourcesUtils.getColor(R.color.grey_88));
            holder.mAuthTV.setEnabled(false);
            if(!TextUtils.isEmpty(carInfo.getDin())){
                if (mCurCarDin.equals(carInfo.getDin())) {
                    holder.mCurCarIV.setBackgroundResource(R.drawable.ic_car_manager_cur_car);
                } else {
                    holder.mCurCarIV.setBackgroundResource(R.drawable.ic_car_manager_uncur_car);
                }
            }
        }

        holder.mStatusTV.setText(String.format(ResourcesUtils.getString(R.string.car_manager_status), status));
        holder.mVinTV.setText(String.format(ResourcesUtils.getString(R.string.car_manager_vin), vin));

        OnViewClick viewClick = new OnViewClick(position);
        holder.mCarInfoLL.setOnClickListener(viewClick);
        holder.mCurCarIV.setOnClickListener(viewClick);
        holder.mAuthTV.setOnClickListener(viewClick);
    }

    @Override
    public int getItemCount() {
        return mCarList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView mStatusIV, mCarIV, mCurCarIV, mAuthIV;
        private TextView mBrandTV, mVinTV, mStatusTV, mAuthTV;
        private LinearLayout mCarInfoLL;

        public MyViewHolder(View itemView) {
            super(itemView);
            mStatusIV = itemView.findViewById(R.id.iv_car_status);
            mCarIV = itemView.findViewById(R.id.iv_car_bg);
            mCurCarIV = itemView.findViewById(R.id.iv_setting_car);
            mAuthIV = itemView.findViewById(R.id.iv_auth);
            mBrandTV = itemView.findViewById(R.id.tv_car_brand);
            mVinTV = itemView.findViewById(R.id.tv_car_vin);
            mStatusTV = itemView.findViewById(R.id.tv_car_status);
            mAuthTV = itemView.findViewById(R.id.tv_setting_car);
            mCarInfoLL = itemView.findViewById(R.id.ll_car_info);
        }
    }

    private class OnViewClick implements View.OnClickListener {
        private int position;

        public OnViewClick(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.ll_car_info) {
                mListener.carDetail(position);
            } else if (id == R.id.iv_setting_car) {
                mListener.settingCurCar(position);
            } else if (id == R.id.tv_setting_car) {
                mListener.verifying(position);
            }
        }
    }

    private ViewClickListener mListener;

    public interface ViewClickListener {
        void carDetail(int position);

        void verifying(int position);

        void settingCurCar(int position);
    }

    public void setViewClickListener(ViewClickListener listener) {
        this.mListener = listener;
    }
}
